﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace WorkflowDesigner.Workflow.Area
{
    public partial class DesignView : WorkflowBase
    {
        public DesignView()
        {
            InitializeComponent();
            OnInit();
        }

        #region 供外观设置用的所有函数

        [ScriptableMember]
        public override void SetFontBold()
        {
        }

        [ScriptableMember]
        public override void SetFontItalics()
        {
        }

        [ScriptableMember]
        public override void SetFontUnderline()
        {
        }

        [ScriptableMember]
        public override void SetFontSize(double dFontSize)
        {
        }

        [ScriptableMember]
        public override void SetFontName(string strFontName)
        {
        }

        public override void SetFontColor(Color clrFont)
        {
        }

        public override void SetFillColor(Color clrFill)
        {
        }

        public override void SetBorderLineStyle(Color clrBorder, double dBorderWidth)
        {
        }

        public override void SetShadowStyle(Color clrShadow, Thickness offset)
        {
        }

        #endregion

        [ScriptableMember]
        public void Save(string strName, string strDescription)
        {
            m_isWorkflowChanged = false;
            HtmlPage.Window.Alert("保存成功！");
        }

        private bool m_isWorkflowChanged = false;
        [ScriptableMember]
        public bool IsWorkflowChanged
        {
            get
            {
                return m_isWorkflowChanged;
            }
        }

        [ScriptableMember]
        public void OnStyleCopy()
        {
        }

        [ScriptableMember]
        public void OnCopy()
        {
        }

        [ScriptableMember]
        public void OnPaste()
        {
        }

        [ScriptableMember]
        public void OnCut()
        {
        }

        [ScriptableMember]
        public void UseChineseUI()
        {
            App.ChangeLangeuage(App.UILanguage.Language_CHS);
        }

        [ScriptableMember]
        public void UseEnglishUI()
        {
            App.ChangeLangeuage(App.UILanguage.Language_ENU);
        }

        [ScriptableMember]
        public void ShowGridLine(bool bShow)
        {
            if (bShow)
            {
                if (lsGridLine == null)
                {
                    lsGridLine = new List<Line>();
                }
                // lsGridLine 中存储了横向和纵向两种线条，他们个数相等
                // 总数除以2便是某一方向上的线条总数
                int nLineCount = lsGridLine.Count / 2;
                double dLineStart = nLineCount * dGridLineSpace;
                SolidColorBrush brush = new SolidColorBrush();
                brush.Color = clrGridLine;
                if (nLineCount > 0)
                {
                    // 已经有的线条全部显示出来
                    foreach (Line item in lsGridLine)
                    {
                        item.Stroke = brush;
                        item.Visibility = Visibility.Visible;
                    }
                }
                int nLineNeed = (int)Math.Max(
                    LayoutRoot.Width / dGridLineSpace, LayoutRoot.Height / dGridLineSpace) + 1;
                if (nLineNeed > nLineCount)
                {
                    // 已经存在的线条还不足以覆盖整个控件
                    // 增加一部分线条填补空白区域
                    for (int nStart = nLineCount; nStart < nLineNeed; nStart++)
                    {
                        // 竖线
                        Line gridLineV = new Line();
                        gridLineV.X1 = nStart * dGridLineSpace;
                        gridLineV.Y1 = 0;
                        gridLineV.X2 = nStart * dGridLineSpace;
                        gridLineV.Y2 = LayoutRoot.Height;
                        gridLineV.Stroke = brush;
                        gridLineV.StrokeThickness = 0.3;
                        LayoutRoot.Children.Add(gridLineV);
                        lsGridLine.Add(gridLineV);
                        gridLineV.SetValue(Canvas.ZIndexProperty, 1);
                        // 横线
                        Line gridLineH = new Line();
                        gridLineH.X1 = 0;
                        gridLineH.Y1 = nStart * dGridLineSpace;
                        gridLineH.X2 = LayoutRoot.Width;
                        gridLineH.Y2 = nStart * dGridLineSpace;
                        gridLineH.Stroke = brush;
                        gridLineH.StrokeThickness = 0.3;
                        LayoutRoot.Children.Add(gridLineH);
                        lsGridLine.Add(gridLineH);
                        gridLineH.SetValue(Canvas.ZIndexProperty, 1);
                    }
                }
                m_isGridLineShow = true;
            }
            else if (m_isGridLineShow && (lsGridLine != null))
            {
                // 不需要显示网格并且网格曾经显示过
                // 全部隐藏
                foreach (Line item in lsGridLine)
                {
                    item.Visibility = Visibility.Collapsed;
                }
                m_isGridLineShow = false;
            }
        }

        // 是否已经显示了网格
        private bool m_isGridLineShow = true;
        // 网格线条列表
        private List<Line> lsGridLine = null;
        // 网格间隔
        private double dGridLineSpace = 40.0;
        // 网格线条颜色
        private Color clrGridLine = Color.FromArgb(255, 160, 160, 160);

        [ScriptableMember]
        public bool IsGridLineShow
        {
            get
            {
                return m_isGridLineShow;
            }
        }
    }
}
